package com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageLocationBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageGetLocationRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/21 15:59
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageManageActivity extends BaseActivity {

    private TextView title , nextStep , location , locationId , purchaseOrder ;
    private List<CYChangeStorageLocationBean.LocationBean.LocationAryBean> aryBeanList;
    private CYChangeStorageBean changeStorageBean;
    private boolean mFlag;
    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_change_storage_manage);
        initView(CYChangeStorageManageActivity.this);
        SPUtils.removeChangeStorageInformation(CYChangeStorageManageActivity.this);
        initData(CYChangeStorageManageActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        changeStorageBean = CYChangeStorageUtils.getChangeStorageBean(CYChangeStorageManageActivity.this);
        if (changeStorageBean.getPurchaseOrderBean() != null && changeStorageBean.getPurchaseOrderBean().size() != 0) {
            purchaseOrder.setText("已选择");
        }else {
            purchaseOrder.setText("请选择");
        }
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        location = (TextView) activity.findViewById(R.id.location);
        locationId = (TextView) activity.findViewById(R.id.locationId);
        purchaseOrder = (TextView) activity.findViewById(R.id.purchaseOrder);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_change_storage_manage_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_change_storage_manager_home_page, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(CYChangeStorageManageActivity.this, getResources().getString(R.string.activity_change_storage_manager_fill_out_the_information), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (TextUtils.isEmpty(locationId.getText().toString())) {
                    Utils.middleToast(activity , "请选择库存地点");
                    return;
                }
                if (changeStorageBean.getPurchaseOrderBean() == null || changeStorageBean.getPurchaseOrderBean().size() == 0) {
                    Utils.middleToast(activity , "请选择采购订单");
                    return;
                }
                startActivity(new Intent(CYChangeStorageManageActivity.this , CYChangeStorageMaterielListActivity.class));
            }
        });
        getExtra(activity);
        location.setText("请选择");
        purchaseOrder.setText("请选择");
        if (mFlag) {
//            Intent intent = new Intent(activity, UpFrame_ManageActivity.class);
//            startActivity(intent);
//            finish();
        } else {
//            getData(activity);
        }
        location.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getCYChangeStorageLocationDialog(activity, aryBeanList, new DialogUtils.CYChangStorageSelectLocationListener() {
                    @Override
                    public void onSelect(Dialog dialog, String code, String name , String id) {
                        location.setText(code + "-" + name);
                        locationId.setText(id);
                        CYChangeStorageUtils.saveLocation(activity , code , name , id);
                    }
                }).show();
            }
        });
        purchaseOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(activity , CYChangeStorageSearchPurchaseOrderActivity.class));
            }
        });
        getData(activity);
    }
    private void getExtra(Activity activity) {
        mFlag = activity.getIntent().getBooleanExtra(ChangeStorageExtra.FLAG, false);
    }

    private void getData (Context context) {
        CYChangeStorageGetLocationRequest.request(context, new GsonBeanListener<CYChangeStorageLocationBean>() {
            @Override
            public void onSuccess(CYChangeStorageLocationBean bean) {
                aryBeanList = bean.getBody().get(0).getLocation_ary();
            }
        });
    }


    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(CYChangeStorageManageActivity.this, getResources().getString(R.string.activity_change_storage_manager_fill_out_the_information), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }
}
